<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8" />
	<title>bpmnjs使用</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<!--css-->
	<link rel="stylesheet" href="css/diagram-js.css" />
	<link rel="stylesheet" href="css/bpmn.css" />
	<link rel="stylesheet" href="css/elementUi.css">
	<link rel="stylesheet" href="css/yigo.css">
	<!--js-->
	<script type="text/javascript" src="js/vue.min.js"></script>
	<script type="text/javascript" src="js/elementUi.js"></script>
	<script type="text/javascript" src="js/jquery.js"></script>
	<script type="text/javascript" src="js/bpmn.js"></script>
	<script type="text/javascript" src="js/translate.js"></script>
	<script type="text/javascript" src="js/bpmn-ext.js"></script>
</head>

<body>
	<div class="modeler" id="app">
		<div id="canvas"></div>
		<div id="properties" class="property-panel">
			<el-tabs v-model="activeName" @tab-click="changeTab" style="margin-left:10px;margin-top:-20px;">
				<el-tab-pane label="流程属性" name="processAttr">
					<el-form :inline="true" :model="formProcess" label-width="160px" size="small">
						<el-form-item label="标识">
							<el-input v-model="formProcess.id" disabled></el-input>
						</el-form-item>
						<el-form-item label="名称">
							<el-input v-model="formProcess.name" @input="nameChange"></el-input>
						</el-form-item>
						<el-form-item label="版本">
							<el-input v-model="formProcess.version" @input="nameChange"></el-input>
						</el-form-item>
						<el-form-item label="迁移表设置">
							<el-input v-model="formProcess.version" @input="nameChange"></el-input>
						</el-form-item>
						<el-form-item label="设置权限的表单">
							<el-input v-model="formProcess.version" @input="nameChange"></el-input>
						</el-form-item>
						<el-form-item label="权限设置">
							<el-input v-model="formProcess.version" @input="nameChange"></el-input>
						</el-form-item>
						<el-form-item label="忽略表单状态">
							<el-input v-model="formProcess.version" @input="nameChange"></el-input>
						</el-form-item>
						<el-form-item label="查询撤销工作项信息">
							<el-input v-model="formProcess.version" @input="nameChange"></el-input>
						</el-form-item>
						<el-form-item label="表单方向模板定义">
							<el-input v-model="formProcess.version" @input="nameChange"></el-input>
						</el-form-item>
						<el-form-item label="关闭实例触发器">
							<el-input v-model="formProcess.version" @input="nameChange"></el-input>
						</el-form-item>
						<el-form-item label="驳回后是否锁定工作项">
							<el-input v-model="formProcess.version" @input="nameChange"></el-input>
						</el-form-item>
						<el-form-item label="自定义流程图信息拓展类">
							<el-input v-model="formProcess.version" @input="nameChange"></el-input>
						</el-form-item> 
						<el-form-item label="不同时机触发事件的拓展类">
							<el-input v-model="formProcess.version" @input="nameChange"></el-input>
						</el-form-item>
					</el-form>
				</el-tab-pane>
				<el-tab-pane label="节点属性" name="nodeAttr">
					<el-form :inline="true" :model="form" label-width="100px" size="small">
						<el-form-item label="节点ID">
							<el-input v-model="form.id" disabled></el-input>
						</el-form-item>
						<el-form-item label="节点名称">
							<el-input v-model="form.name" @input="nameChange"></el-input>
						</el-form-item>
						<!-- 任务节点允许选择人员 -->
						<el-form-item label="节点人员" v-if="userTask">
							<el-select v-model="form.userType" placeholder="请选择" @change="typeChange">
								<el-option value="assignee" label="指定人员"></el-option>
								<el-option value="candidateUsers" label="候选人员"></el-option>
								<el-option value="candidateGroups" label="角色/岗位"></el-option>
							</el-select>
						</el-form-item>
						<!-- 指定人员 -->
						<el-form-item label="指定人员" v-if="userTask && form.userType === 'assignee'">
							<el-select v-model="form.assignee" placeholder="请选择" key="1"
								@change="(value) => addUser({assignee: value})">
								<el-option v-for="item in users" :key="item.value" :label="item.label"
									:value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
						<!-- 候选人员 -->
						<el-form-item label="候选人员" v-else-if="userTask && form.userType === 'candidateUsers'">
							<el-select v-model="form.candidateUsers" placeholder="请选择" key="2" multiple
								@change="(value) => addUser({candidateUsers: value.join(',') || value})">
								<el-option v-for="item in users" :key="item.value" :label="item.label"
									:value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
						<!-- 角色/岗位 -->
						<el-form-item label="角色/岗位" v-else-if="userTask && form.userType === 'candidateGroups'">
							<el-select v-model="form.candidateGroups" placeholder="请选择"
								@change="(value) => addUser({candidateGroups: value})">
								<el-option v-for="item in roles" :key="item.value" :label="item.label"
									:value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
						<!-- 分支允许添加条件 -->
						<el-form-item label="分支条件" v-if="sequenceFlow">
							<el-select v-model="form.user" placeholder="请选择">
								<el-option v-for="item in users" :key="item.value" :label="item.label"
									:value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-form>
				</el-tab-pane>
			</el-tabs>
		</div>

		<div class="toolbar">
			<a title="download" style="display:none">下载</a>
			<a ref="saveDiagram" href="javascript:">下载BPMN图</a>
		</div>
	</div>
	<script>
		new Vue({
			el: '#app',
			computed: {
				userTask() {
					if (!this.element) {
						return;
					}
					return this.element.type === "bpmn:UserTask" || this.element.type === "bpmn:Task";
				},
				sequenceFlow() {
					if (!this.element) {
						return;
					}
					return this.element.type === "bpmn:SequenceFlow";
				}
			},
			data() {
				return {
					activeName: 'processAttr',
					container: null,
					modeler: null,
					customTranslateModule: null,
					xml: `
					<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:bpmn2="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="sample-diagram" targetNamespace="http://bpmn.io/schema/bpmn" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
  <bpmn2:process id="process1567044459787" name="流程1567044459787">
    <bpmn2:documentation>描述</bpmn2:documentation>
    <bpmn2:startEvent id="StartEvent_01ydzqe" name="开始">
      <bpmn2:outgoing>SequenceFlow_1qw929z</bpmn2:outgoing>
    </bpmn2:startEvent>
    <bpmn2:sequenceFlow id="SequenceFlow_1qw929z" sourceRef="StartEvent_01ydzqe" targetRef="Task_1piqdk6" />
    <bpmn2:userTask id="Task_1piqdk6" name="请假申请" assignee="zhangsan" userType="assignee">
      <bpmn2:incoming>SequenceFlow_1qw929z</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_11h4o22</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:exclusiveGateway id="ExclusiveGateway_0k39v3u" name="判断">
      <bpmn2:incoming>SequenceFlow_11h4o22</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_1iu7pfe</bpmn2:outgoing>
      <bpmn2:outgoing>SequenceFlow_04uqww2</bpmn2:outgoing>
    </bpmn2:exclusiveGateway>
    <bpmn2:sequenceFlow id="SequenceFlow_11h4o22" sourceRef="Task_1piqdk6" targetRef="ExclusiveGateway_0k39v3u" />
    <bpmn2:sequenceFlow id="SequenceFlow_1iu7pfe" sourceRef="ExclusiveGateway_0k39v3u" targetRef="Task_10fqcwp" />
    <bpmn2:userTask id="Task_10fqcwp" name="经理审批" assignee="zhangsan" userType="assignee">
      <bpmn2:incoming>SequenceFlow_1iu7pfe</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_1xod8nh</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:sequenceFlow id="SequenceFlow_04uqww2" sourceRef="ExclusiveGateway_0k39v3u" targetRef="Task_15n23yh" />
    <bpmn2:userTask id="Task_15n23yh" name="总部审批">
      <bpmn2:incoming>SequenceFlow_04uqww2</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_0c8wrs4</bpmn2:outgoing>
    </bpmn2:userTask>
    <bpmn2:exclusiveGateway id="ExclusiveGateway_1sq33g6" name="汇总">
      <bpmn2:incoming>SequenceFlow_0c8wrs4</bpmn2:incoming>
      <bpmn2:incoming>SequenceFlow_1xod8nh</bpmn2:incoming>
      <bpmn2:outgoing>SequenceFlow_0h8za82</bpmn2:outgoing>
    </bpmn2:exclusiveGateway>
    <bpmn2:sequenceFlow id="SequenceFlow_0c8wrs4" sourceRef="Task_15n23yh" targetRef="ExclusiveGateway_1sq33g6" />
    <bpmn2:sequenceFlow id="SequenceFlow_1xod8nh" sourceRef="Task_10fqcwp" targetRef="ExclusiveGateway_1sq33g6" />
    <bpmn2:endEvent id="EndEvent_0pnmjd3">
      <bpmn2:incoming>SequenceFlow_0h8za82</bpmn2:incoming>
    </bpmn2:endEvent>
    <bpmn2:sequenceFlow id="SequenceFlow_0h8za82" sourceRef="ExclusiveGateway_1sq33g6" targetRef="EndEvent_0pnmjd3" />
  </bpmn2:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="process1567044459787">
      <bpmndi:BPMNEdge id="SequenceFlow_0h8za82_di" bpmnElement="SequenceFlow_0h8za82">
        <di:waypoint x="550" y="565" />
        <di:waypoint x="550" y="602" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_1xod8nh_di" bpmnElement="SequenceFlow_1xod8nh">
        <di:waypoint x="680" y="460" />
        <di:waypoint x="680" y="540" />
        <di:waypoint x="575" y="540" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_0c8wrs4_di" bpmnElement="SequenceFlow_0c8wrs4">
        <di:waypoint x="430" y="460" />
        <di:waypoint x="430" y="540" />
        <di:waypoint x="525" y="540" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_04uqww2_di" bpmnElement="SequenceFlow_04uqww2">
        <di:waypoint x="525" y="300" />
        <di:waypoint x="430" y="300" />
        <di:waypoint x="430" y="380" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_1iu7pfe_di" bpmnElement="SequenceFlow_1iu7pfe">
        <di:waypoint x="575" y="300" />
        <di:waypoint x="680" y="300" />
        <di:waypoint x="680" y="380" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_11h4o22_di" bpmnElement="SequenceFlow_11h4o22">
        <di:waypoint x="550" y="230" />
        <di:waypoint x="550" y="275" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_1qw929z_di" bpmnElement="SequenceFlow_1qw929z">
        <di:waypoint x="550" y="108" />
        <di:waypoint x="550" y="150" />
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="StartEvent_01ydzqe_di" bpmnElement="StartEvent_01ydzqe">
        <dc:Bounds x="532" y="72" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="539" y="53" width="22" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="UserTask_1qxjy46_di" bpmnElement="Task_1piqdk6">
        <dc:Bounds x="500" y="150" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="ExclusiveGateway_0k39v3u_di" bpmnElement="ExclusiveGateway_0k39v3u" isMarkerVisible="true">
        <dc:Bounds x="525" y="275" width="50" height="50" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="539" y="332" width="23" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="UserTask_18pwui1_di" bpmnElement="Task_10fqcwp">
        <dc:Bounds x="630" y="380" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="UserTask_1j0us24_di" bpmnElement="Task_15n23yh">
        <dc:Bounds x="380" y="380" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="ExclusiveGateway_1sq33g6_di" bpmnElement="ExclusiveGateway_1sq33g6" isMarkerVisible="true">
        <dc:Bounds x="525" y="515" width="50" height="50" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="539" y="485" width="23" height="14" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="EndEvent_0pnmjd3_di" bpmnElement="EndEvent_0pnmjd3">
        <dc:Bounds x="532" y="602" width="36" height="36" />
      </bpmndi:BPMNShape>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn2:definitions>
      `,
					formProcess: {
						id: "workflowTest",
						name: "测试工作流",
						version: "1"
					},
					form: {
						id: "",
						name: ""
					},
					element: {},
					users: [{
							value: "zhangsan",
							label: "张三"
						},
						{
							value: "lisi",
							label: "李四"
						},
						{
							value: "wangwu",
							label: "王五"
						}
					],
					roles: [{
							value: "manager",
							label: "经理"
						},
						{
							value: "personnel",
							label: "人事"
						},
						{
							value: "charge",
							label: "主管"
						}
					]
				};
			},
			mounted() {
				this.customModule = {
					translate: ['value', customTranslate],//此处为汉化对象
					paletteProvider: ['type', CustomPaletteProvider], //自定义paletteProvider
					//contextPadProvider: ['type', CustomContextPadProvider], //自定义contextPadProvider
					bendpoints: ["value", ""],// 禁止拖动线
					zoomScroll: ["value", ""],// 禁用滚轮滚动
					contextPadProvider: ["value", ""],// 禁止点击节点出现contextPad
					labelEditingProvider: ["value", ""] // 禁止双击节点出现label编辑框
				};
				this.container = document.getElementById('canvas');
				this.modeler = new window.BpmnJS({
					container: this.container,
					additionalModules: [this.customModule],
					keyboard:{
						bindTo:window
					}
				});

				this.create();
				this.handleModeler();
				this.handleDownload();
			},
			methods: {
				//切换tab
				changeTab(tab, event) {
					console.log(tab, event);
				},
				create() {
					this.modeler.createDiagram();
					this.modeler.importXML(this.xml);
				},
				handleModeler() {
					// 监听节点选择变化
					this.modeler.on("selection.changed", e => {
						const element = e.newSelection[0];
						this.element = element;
						console.log(this.element);
						if (!element) return;
						this.form = {
							...element.businessObject,
							...element.businessObject.$attrs
						};
						if (this.form.userType === "candidateUsers") {
							this.form["candidateUsers"] =
								this.form["candidateUsers"].split(",") || [];
						}
					});

					//  监听节点属性变化
					this.modeler.on("element.changed", e => {
						const {
							element
						} = e;
						if (!element) return;
						//  新增节点需要更新回属性面板
						if (element.id === this.form.id) {
							this.form.name = element.businessObject.name;
							this.form = {
								...this.form
							};
						}
					});

				},
				// 属性面板名称，更新回流程节点
				nameChange(name) {
					const modeling = this.modeler.get("modeling");
					modeling.updateLabel(this.element, name);
				},
				// 任务节点配置人员
				addUser(properties) {
					this.updateProperties(
						Object.assign(properties, {
							userType: Object.keys(properties)[0]
						})
					);
				},
				// 切换人员类型
				typeChange() {
					const types = ["assignee", "candidateUsers", "candidateGroups"];
					types.forEach(type => {
						delete this.element.businessObject.$attrs[type];
						delete this.form[type];
					});
				},
				// 在这里我们封装一个通用的更新节点属性的方法
				updateProperties(properties) {
					const modeling = this.modeler.get("modeling");
					modeling.updateProperties(this.element, properties);
				},
				// 下载文件
				handleDownload() {
					// 获取a标签dom节点
					const downloadLink = this.$refs.saveDiagram;
					// 监听流程图改变事件
					const _this = this;
					this.modeler.on("commandStack.changed", function () {
						_this.saveDiagram(function (err, xml) {
							_this.setEncoded(downloadLink, "diagram.bpmn", err ? null : xml);
						});
					});
				},
				// 下载为SVG格式,done是个函数，调用的时候传入的
				saveDiagram(done) {
					// 把传入的done再传给bpmn原型的saveXML函数调用
					this.modeler.saveXML({
						format: true
					});
				},
				// 当图发生改变的时候会调用这个函数，这个data就是图的xml
				setEncoded(link, name, data) {
					// 把xml转换为URI，下载要用到的
					const encodedData = encodeURIComponent(data);
					// 获取到图的xml，保存就是把这个xml提交给后台
					this.xml = data;
					// 下载图的具体操作,改变a的属性，className令a标签可点击，href令能下载，download是下载的文件的名字
					if (data) {
						link.className = "active";
						link.href = "data:application/bpmn20-xml;charset=UTF-8," + encodedData;
						link.download = name;
					}
				}
			}
		})
	</script>
</body>

</html>